$().ready(function() {

	$.ajax({
		cache : true,
		type : "GET",
		url : "/sys/user/competitionArea",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				let str = '';
				data.data?.map(function (v,k){
					if( k == 0 ) str += `<option>请选择</option>`
					str += `<option value="${v.id}">${v.name}</option>`
				})
				$("#area").html(str);

				$("#area").change(function (ev){
					// console.log(ev.target.selectedIndex)
					$("#competitionAreaName").val($("#area option:selected").html());
					if( $("#area option:selected").html() == '请选择' ) 	$("#competitionAreaName").val('');
				})

			} else {
				layer.alert(data.msg)
			}

		}
	});

	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

$("#job").hide();

$("#userType").change(function (v){
	let str = `
			<div class="form-group">
				<label class="col-sm-3 control-label">寸照：</label>
				<div class="col-sm-8">
					<button type="button" class="layui-btn" id="test1">上传图片</button>
					<input type="hidden" name="img" id="img">
					<div class="layui-upload-list" id="demo1">
						<img class="layui-upload-img">
					</div>
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">职称：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" name="professional" placeholder="请输入您的职称">
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">职务：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" name="job" placeholder="请输入您的职务">
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">开户行：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" name="accountName" placeholder="请输入您的开户行">
				</div>
			</div>
			
			<div class="form-group">
				<label class="col-sm-3 control-label">银行卡号：</label>
				<div class="col-sm-8">
					<input type="text" class="form-control" name="cardNumber" placeholder="请输入您的银行卡号">
				</div>
			</div>
		`;
	if( $(this).val() == 3 ){
		$("#job").show().html(str);

		layui.use(['upload'], function() {
			var upload = layui.upload;
			let index;
			upload.render({
				elem: '#test1'
				,accept: ''
				,file:''
				,url: '/common/sysFile/upload'
				,choose: function(obj){
					console.log($("."+this.id).attr('class'))
					index = layer.load(1,{fixed:false,offset: 'auto'});
				}
				, done: function ( res ) {
					layer.close(index);
					$("#demo1").show();
					$("#demo1").find('img').attr('src', res.data );
					$("#img").val(res.data)
				}
				,error:function (res){
					layer.close(index);
					layer.msg('上传失败，请检查')
				}
			})
		})

	}else{
		$("#job").html('').hide();
	}

})


let xmSele = [{name:'教师组',value:1},{name:'学生中职组',value:2},{name:'学生高校组',value:3}];
xmSelect.render({
	el: '#encipher',
	autoRow: true,
	name: '',
	max:3,
	initValue: [] ,
	data: xmSele,
	maxMethod(seles, item) {
		layer.msg(`最多只能选择3个`)
	},
	on: function (data) {
		let arr = [];
		data.arr.map(function (v){
			arr.push(v.value);
		})
		$(".competitionType").val( arr.join(',').replace(/,/g,';;') );
	}
})

function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function save() {

	// $("#track").val( $("#track").val().toUpperCase() );

	$("#roleIds").val(getCheckedRoles());
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/user/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			userType: {
				required: true,
			},
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2,
				remote : {
					url : "/sys/user/exist", // 后台处理程序
					type : "post", // 数据发送方式
					dataType : "json", // 接受数据格式
					data : { // 要传递的数据
						username : function() {
							return $("#username").val();
						}
					}
				}
			},
			password : {
				required : true,
				minlength : 6
			},
			type : {
				required : true
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			email : {
				// required : true,
				email : true
			},
			mobile : {
				isPhone:true,
				required : true,
				remote : {
					url : "/sys/user/exist", // 后台处理程序
					type : "post", // 数据发送方式
					dataType : "json", // 接受数据格式
					data : { // 要传递的数据
						mobile : function() {
							return $("#mobile").val();
						}
					}
				}
			},
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			agree : "required",
			competitionType:'required',
			track:'required',
			professional:'required',
			job:'required',
			accountName:'required',
			cardNumber:'required',
			refereesNumber:'required'
		},
		messages : {
			userType:{
				required : icon + "请选择类型",
			},
			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上",
				remote : icon + "用户名已经存在"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			},
			mobile : {
				required : icon + "请输入手机号码",
				remote : icon + "手机号码已经存在"
			},
			competitionType:{
				required : icon + "请选择你的所属竞赛",
			},
			track:{
				required : icon + "必填",
			},
			professional:{
				required : icon + "必填"
			},
			job:{
				required : icon + "必填"
			},
			accountName:{
				required : icon + "必填"
			},
			cardNumber:{
				required : icon + "必填"
			},
			refereesNumber:{
				required : icon + "必填"
			}
		}
	})
}

var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"/sys/dept/treeView"
	})
}

function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}
